package com.cs.beneficios.web.action;
//import com.cs.beneficios.business.entity.AtributosDAO;
import com.cs.beneficios.business.entity.RestriccionDAO;
import com.cs.beneficios.common.ConnectionFactory;
import java.sql.SQLException;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForward;
//import org.apache.struts.action.ActionError; 
//import org.apache.struts.action.ActionErrors;
import java.io.IOException;
import javax.servlet.ServletException;

public class RestricModificadaAction extends Action 
{
  /**
   * This is the main action called from the Struts framework.
   * @param mapping The ActionMapping used to select this instance.
   * @param form The optional ActionForm bean for this request.
   * @param request The HTTP Request we are processing.
   * @param response The HTTP Response we are processing.
   * @return 
   * @throws java.io.IOException
   * @throws javax.servlet.ServletException
   */
  public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
  {
    HttpSession session = request.getSession();
    String strCodRestric = (String)session.getAttribute("codrestric");
    String sub = (String)session.getAttribute("subpro");
    session.setAttribute("subprograma",sub);
    
    int continuar = 0;
    Object oboperando1 = request.getParameter("operando1");
    String strOperando1 = "";
    String strOperando = "";
    String strCampo1 = "";
    if(oboperando1 == null)
    {
      strOperando = request.getParameter("descVflex");
      if(!strOperando.equals("nulo"))
      {
        String ArrOperando[] = strOperando.split("~");
        
        String stroper =  ArrOperando[0];
        if(!stroper.equals("null"))
        {
          strOperando1 = ArrOperando[0];
          strCampo1 = ArrOperando[1];
        }else
        {
          strOperando1 = ArrOperando[1];
          if(strOperando1.equals("nulo"))
          {
            continuar = 1;
          }
        }  
      }else
      {
         request.setAttribute("msg2","No se selecciono ninguna opcion de la lista de valores");
         request.setAttribute("msg1","La restriccion no se ingreso correctamente");
      }
    }else
    {
      strOperando1 = request.getParameter("operando1");
    }
            
    if (continuar == 0)
    {
      
      
      String strOperador = request.getParameter("operador");
      
      String strOperando2 = request.getParameter("operando2");
      String strDescripcion = request.getParameter("descripcion");
      String strAlerta = request.getParameter("alerta");
      String strVigencia = request.getParameter("vigencia");
      String strArr[] = {strDescripcion,strOperador,strOperando1,strOperando2,strAlerta,strVigencia,strCampo1};
      
      //ConnectionFactory cf = new ConnectionFactory("beneficios","beneficios");
	  ConnectionFactory cf = new ConnectionFactory();
      RestriccionDAO restDAO = new RestriccionDAO(cf);

      try
      {
        
        int iModificacionBien = restDAO.ModificarRestriccion(strCodRestric,strArr);   
        cf.closeConnection(restDAO.getConn());
        if(iModificacionBien!=0)
        {
          session.setAttribute("msg","La restriccion fue modificada correctamente");
        }else
        {
          session.setAttribute("msg","Ocurrio un error al modificar la restriccion");
          request.setAttribute("msg1","La restriccion no se modifico correctamente");
        }
            
      }catch(SQLException e){
		System.err.println("/******** TDSUB - cwsub");
		e.printStackTrace();
		System.err.println("TDSSPE - cwsub ********/");
      }
    }else
    {
      request.setAttribute("msg1","La restriccion no se modifico correctamente");
    }
      
    return mapping.findForward("success");
  }
}